
/*
  Author:
  Lu Yiming <lu.yiming.lu@gmail.com>
 */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

/* for compile, uint16_t */
#include <netdb.h>


#include "usage.h"
#include "udp_local.h"
#include "udp_ipv4.h"
#include "shm_sysv.h"
#include "shm_sysv2.h"


void usage(void)
{
	printf("Usage:\n"
	       "conn [-h, --help]\n"
	       "[shm-sysv client [packets n] [full-duplex/half-duplex]]\n"
	       "[shm-sysv server [debug]]\n"
	       "[shm-sysv2 client [packets n] [full-duplex/half-duplex]]\n"
	       "[shm-sysv2 server [debug]]\n"
	       "[udp-local client [packets n] [full-duplex/half-duplex]]\n"
	       "[udp-local server [port n] [debug]]\n"
	       "[udp-ipv4 client [server host] [port n] [packets n] [full-duplex/half-duplex]]\n"
	       "[udp-ipv4 server [debug]]\n"
		);
}

int conn_init()
{
	int rt;

	rt = data_init();
	if (rt)
		return rt;

	return 0;
}

int main(int argc, char **argv)
{
	if ((2 == argc) &&
	    ((0 == strcmp(argv[1], "-h")) ||
	     (0 == strcmp(argv[1], "--help")))) {
		usage();
		return 0;
	} 

	else if ((argc > 2) &&
	    (0 == strcmp(argv[1], "udp-local")) &&
	    (0 == strcmp(argv[2], "client"))) {
		if (conn_init()) {
			perror("initi error.\n");
			return 1;
		}
		udp_local_client(argc - 3, argv + 3);
	} else if ((argc > 2) &&
	    (0 == strcmp(argv[1], "udp-local")) &&
	    (0 == strcmp(argv[2], "server"))) {
		udp_local_server(argc - 3, argv + 3);
	} 

	else if ((argc > 2) &&
	    (0 == strcmp(argv[1], "udp-ipv4")) &&
	    (0 == strcmp(argv[2], "client"))) {
		if (conn_init()) {
			perror("initi error.\n");
			return 1;
		}
		udp_ipv4_client(argc - 3, argv + 3);
	} else if ((argc > 2) &&
	    (0 == strcmp(argv[1], "udp-ipv4")) &&
	    (0 == strcmp(argv[2], "server"))) {
		udp_ipv4_server(argc - 3, argv + 3);
	} 

	else if ((argc > 2) &&
	    (0 == strcmp(argv[1], "shm-sysv")) &&
	    (0 == strcmp(argv[2], "client"))) {
		if (conn_init()) {
			perror("initi error.\n");
			return 1;
		}
		shm_sysv_client(argc - 3, argv + 3);
	} else if ((argc > 2) &&
	    (0 == strcmp(argv[1], "shm-sysv")) &&
	    (0 == strcmp(argv[2], "server"))) {
		shm_sysv_server(argc - 3, argv + 3);
	} 

	else if ((argc > 2) &&
	    (0 == strcmp(argv[1], "shm-sysv2")) &&
	    (0 == strcmp(argv[2], "client"))) {
		if (conn_init()) {
			perror("initi error.\n");
			return 1;
		}
		shm_sysv2_client(argc - 3, argv + 3);
	} else if ((argc > 2) &&
	    (0 == strcmp(argv[1], "shm-sysv2")) &&
	    (0 == strcmp(argv[2], "server"))) {
		shm_sysv2_server(argc - 3, argv + 3);
	} 

	else {
		usage();
	}

	return 0;
}
